//
//  ASViewControllerDemo.swift
//  soapp_Example
//
//  Created by zhu xietong on 2018/2/13.
//  Copyright © 2018年 CocoaPods. All rights reserved.
//

import UIKit
import AsyncDisplayKit
import soapp
import Eelay


class ToolKit:JoTouchNode{
    
    let bt = ASButtonNode()
    override func addLayoutRules() {
//        bt.style.width = ASDimension(unit: .points, value: 100)
//        bt.style.height = ASDimension(unit: .points, value: 80)
        bt.setAttributedTitle("SSSSHHHHHHH".ui.cl_666.font42.astring, for: .normal)

    }
    
//    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
//        let nameLocationStack = ASStackLayoutSpec.vertical()
//        nameLocationStack.style.flexShrink = 1.0
//        nameLocationStack.style.flexGrow = 1.0
//        backgroundColor = .red
//
//
//
//
//        return ASInsetLayoutSpec(insets: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10), child: bt)
//    }
//
//    override func addLayoutRules() {
////        _ = self["20",40]
//
//        mf.spec = .node(bt)
//        bt.setAttributedTitle(mo.model["name","XSS"].ui.cl_222.center.astring, for: .normal)
//        backgroundColor = .red
//        bt.backgroundColor = .blue
//    }
//
//    override func layoutThatFits(_ constrainedSize: ASSizeRange) -> ASLayout {
//        <#code#>
//    }
//    override func mo_model_reload() {
//        bt.setAttributedTitle(mo.model["name","X"].ui.cl_222.center.astring, for: .normal)
////        bt.backgroundColor = .blue
//    }
}

class MyNode: MoDisplayNode{
    let node1 = ASDisplayNode()
    let node2 = ASDisplayNode()
    let node3 = ASDisplayNode()
    let node4 = ASDisplayNode()
    let node5 = ASDisplayNode()
    let node6 = ASDisplayNode()
    
    let text1 = ToolKit()
    let text2 = ToolKit()


    
    

var __spec:_Spec = .absolute([],childs: [_Spec]())
    {
        didSet{
            let nodes = __spec.specAndNodes.1
            for one in nodes{
                addSubnode(one)
            }
            
        }
    }
   
    var nodes = [ASDisplayNode]()
    
    
    override func addLayoutRules() {
        node1.backgroundColor = .red
        node2.backgroundColor = .blue
        node3.backgroundColor = .brown
        node4.backgroundColor = .green
        node5.backgroundColor = .yellow

        node6.backgroundColor = .black

//        super.addLayoutRules()

        backgroundColor = UIColor(shex:"#ccc8")
        
        __spec = .stack(["v|10",&-.center,"id|list"], childs: [
//            text1.Spec,
//            text2.Spec,
        ])

        addSubnode(text1)
        addSubnode(text2)
        
         nodes = [text1,text2]
        
//        __spec = [
//            "v|10",[10],&-.center,_justfy(.center),[
//                node1[20,"30"],
//
//            ]
//        ]

//        node1.style.width = ASDimension(unit: .points, value: 100)
//        node1.style.height = ASDimension(unit: .points, value: 80)


//        __spec = .stack([[20]], childs: [
//            .back([], over: .center([100,"100"], xy: .xy, child: node1["40",40]), back: node2[80,"90"])
//            ])
        
//        __spec = .stack([], childs: [
//            .center(xy: .xy, child: .back(over: node1["50",50,[10]], back: .node(node2))),
//            node5["40",60]
//        ])
        
//        __spec = .center(xy: .x, child: .ratio(ratio: 2, child: .node(node4), rules:["60"]))
//        __spec = .center([],xy: .xy, child: node4.Spec)
        
        
//        __spec = .stack([[7],"v|10",&-.center,&=.space_around,], childs: [
//            node1[80,"20"],
//            node2[80,"37"],
//            node3[50,"50"],
//            node4[80,"20"],
//
//        ])

//        __spec = .stack([[70,10,10,10],"100",60,"v|10"], childs: [
//            node1[40,"110"],
//            node2[30,"36"],
////            node4[40,"55",[4,8]],
//            .back(back: .node(node3), over: node4[40,"55",[4,8]])
//        ])
        
//        flexRules = [
//            _padding(0),_spec(.back(rules: [
//                node1,_spec(.center(.y)),"20",30,
//            ]))
//        ]
        

        
        
    }
    
    
    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        let spec = __spec.laySpec
        if let list = spec.subSpec(specId: "list") as? ASStackLayoutSpec
        {
            list.children =  nodes
        }
        return spec
    }
   
    
//    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
//
//
//        //        let photoDimension: CGFloat = constrainedSize.max.width / 4.0
//        //        photoNode.style.preferredSize = CGSize(width: photoDimension, height: photoDimension)
//        //        photoNode.image = #imageLiteral(resourceName: "FCWC饰品")
//        //        photoNode.style.minWidth = ASDimension(unit: .points, value: 100)
//        //        photoNode.style.height = ASDimension(unit: .points, value: 200)
//        //        photoNode.backgroundColor = .blue
//        //
//        //        // INFINITY is used to make the inset unbounded
//        //        let insets = UIEdgeInsets(top: CGFloat.infinity, left: 12, bottom: 12, right: 12)
//        //        let textInsetSpec = ASInsetLayoutSpec(insets: insets, child: titleNode)
//        //        self.backgroundColor = .red
//        //        titleNode.backgroundColor = .green
//        //
//        ////        return ASOverlayLayoutSpec(child: photoNode, overlay: textInsetSpec)
//
//        let backgroundNode = bNode
//        let foregroundNode = fNode
//
//        let relativeSpec = ASRelativeLayoutSpec(horizontalPosition: .start,
//                                                verticalPosition: .start,
//                                                sizingOption: [],
//                                                child: foregroundNode)
//
//        let backgroundSpec = ASBackgroundLayoutSpec(child: relativeSpec, background: backgroundNode)
//        return backgroundSpec
//    }
}

class ASViewControllerDemo: ASViewController<ASDisplayNode> {
    
    let node1 = MyNode()

    let topV = ASDisplayNode()
    let btV = ASDisplayNode()
    
    public init() {
        
        super.init(node: ASDisplayNode())
        
        self.title = "Layout Example"
        self.node.automaticallyManagesSubnodes = true
        
        topV.backgroundColor = .red

        btV.backgroundColor = .blue
        
        node.backgroundColor = .white
        
    

        self.node.layoutSpecBlock = { [weak self] node, constrainedSize in print("_++++D+D+")
            guard let ws = self else {
                print("_++dd++D+D+00001")
                
                return ASLayoutSpec()
            }
            print("_++++D+D+00002")
            
            return ws.quickLayoutSpec(node: node, constrainedSize: constrainedSize)
            
        }
    }
    
    
    func specGetter() -> _Spec {
        return _Spec.stack(["v|10",&-.stretch], childs: [
            topV[_grow(1),_shrink(1)],
            btV["40"]
            ])
    }

    func quickLayoutSpec(node:ASDisplayNode,constrainedSize:ASSizeRange)->ASLayoutSpec{
        
        let spec = node.fastLayoutSpec(_spec: specGetter())
        print("DDD___S")
        return spec
    }

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


